
package com.echo.controller;

import cn.hutool.core.lang.UUID;
import com.aliyun.oss.OSS;
import com.echo.entity.Exam;
import com.echo.entity.ExamRecord;
import com.echo.result.ResultVO;
import com.echo.service.IExamService;
import com.echo.vo.*;
import com.fasterxml.jackson.core.JsonProcessingException;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;

@RestController
@Tag(name = "Exam APIs")
@RequestMapping("/api/exam")
public class ExamController {
    @Autowired
    private IExamService examService;

    @GetMapping("/question/all")
    @Operation(summary ="获取所有问题的列表，得到题目的所有信息")
    ResultVO<List<QuestionVo>> getQuestionAll() {
        ResultVO<List<QuestionVo>> resultVO;
        try {
            List<QuestionVo> questionAll = examService.getQuestionAll();
            resultVO = new ResultVO<>(0, "获取全部问题列表成功", questionAll);
        } catch (Exception e) {
            e.printStackTrace();
            resultVO = new ResultVO<>(-1, "获取全部问题列表失败", null);
        }
        return resultVO;
    }

    @PostMapping("/question/update")
    @Operation(summary ="更新问题")
    ResultVO<QuestionVo> questionUpdate(@RequestBody QuestionVo questionVo) {
        // 完成问题的更新
        System.out.println(questionVo);
        try {
            QuestionVo questionVoResult = examService.updateQuestion(questionVo);
            return new ResultVO<>(0, "更新问题成功", questionVoResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO<>(-1, "更新问题失败", null);
        }
    }

    @PostMapping("/question/create")
    @Operation(summary ="创建问题")
    ResultVO<String> questionCreate(@RequestBody QuestionCreateSimplifyVo questionCreateSimplifyVo, HttpServletRequest request) {
        QuestionCreateVo questionCreateVo = new QuestionCreateVo();
        // 把能拷贝过来的属性都拷贝过来
        BeanUtils.copyProperties(questionCreateSimplifyVo, questionCreateVo);
        // 设置创建者信息
        String userId = (String) request.getAttribute("user_id");
        questionCreateVo.setQuestionCreatorId(userId);
        System.out.println(questionCreateVo);
        try {
            examService.questionCreate(questionCreateVo);
            return new ResultVO<>(0, "问题创建成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO<>(-1, "创建问题失败", null);
        }
    }

    @GetMapping("/question/selection")
    @Operation(summary ="获取问题分类的相关选项")
    ResultVO<QuestionSelectionVo> getSelections() {
        QuestionSelectionVo questionSelectionVo = examService.getSelections();
        if (questionSelectionVo != null) {
            return new ResultVO<>(0, "获取问题分类选项成功", questionSelectionVo);
        } else {
            return new ResultVO<>(-1, "获取问题分类选项失败", null);
        }
    }

    @GetMapping("/question/detail/{id}")
    @Operation(summary ="根据问题的id获取问题的详细信息")
    ResultVO<QuestionDetailVo> getQuestionDetail(@PathVariable String id) {
        //  根据问题id获取问题的详细信息
        System.out.println(id);
        ResultVO<QuestionDetailVo> resultVO;
        try {
            QuestionDetailVo questionDetailVo = examService.getQuestionDetail(id);
            resultVO = new ResultVO<>(0, "获取问题详情成功", questionDetailVo);
        } catch (Exception e) {
            e.printStackTrace();
            resultVO = new ResultVO<>(-1, "获取问题详情失败", null);
        }
        return resultVO;
    }
    //这里是传过来exam的id
    @GetMapping("/all")
    @Operation(summary ="获取全部考试的列表")
    ResultVO<List<ExamVo>> getExamAll(@RequestParam(required = false) String type) {
        // 需要拼接前端需要的考试列表对象
        ResultVO<List<ExamVo>> resultVO;
        List<ExamVo> examVos;
        try {
            examVos = examService.getExamAll();
          /* todo:分类判断
               if (Objects.equals(type, "null")) {
                examVos = examService.getExamAll();
            } else {
                examVos = examService.getTypeToExam(type);
            }*/
            resultVO = new ResultVO<>(0, "获取全部考试的列表/对应分类的试卷列表成功", examVos);
        } catch (Exception e) {
            e.printStackTrace();
            resultVO = new ResultVO<>(-1, "获取全部考试的列表/对应分类的试卷列表失败", null);
        }
        return resultVO;
    }

    @GetMapping("/question/type/list")
    @Operation(summary ="获取问题列表，按照单选、多选和判断题分类返回")
    ResultVO<ExamQuestionTypeVo> getExamQuestionTypeList() {
        // 获取问题的分类列表
        ResultVO<ExamQuestionTypeVo> resultVO;
        try {
            ExamQuestionTypeVo examQuestionTypeVo = examService.getExamQuestionType();
            resultVO = new ResultVO<>(0, "获取问题列表成功", examQuestionTypeVo);
        } catch (Exception e) {
            e.printStackTrace();
            resultVO = new ResultVO<>(-1, "获取问题列表失败", null);
        }
        return resultVO;
    }

    //这里也可以增加逻辑
    @PostMapping("/create")
    @Operation(summary ="创建考试")
    ResultVO<Exam> createExam(@RequestBody ExamCreateVo examCreateVo, HttpServletRequest request) {
        // 从前端传参数过来，在这里完成考试的入库
        ResultVO<Exam> resultVO;
        String userId = (String) request.getAttribute("user_id");
        try {

            Exam exam = examService.create(examCreateVo, userId);
            resultVO = new ResultVO<>(0, "创建考试成功", exam);
        } catch (Exception e) {
            e.printStackTrace();
            resultVO = new ResultVO<>(-1, "创建考试失败", null);
        }
        return resultVO;
    }

    @PostMapping("/update")
    @Operation(summary ="更新考试")
    ResultVO<Exam> updateExam(@RequestBody ExamVo examVo, HttpServletRequest request) {
        // 从前端传参数过来，在这里完成考试的入库
        ResultVO<Exam> resultVO;
        String userId = (String) request.getAttribute("user_id");
        try {
            Exam exam = examService.update(examVo, userId);
            resultVO = new ResultVO<>(0, "更新考试成功", exam);
        } catch (Exception e) {
            e.printStackTrace();
            resultVO = new ResultVO<>(-1, "更新考试失败", null);
        }
        return resultVO;
    }

    @GetMapping("/card/list")
    @Operation(summary ="获取考试列表，适配前端卡片列表")
    ResultVO<List<ExamCardVo>> getExamCardList(@RequestParam String type,@RequestParam Boolean ifAi) {
        // todo：获取考试列表卡片
        ResultVO<List<ExamCardVo>> resultVO;
        try {
            List<ExamCardVo> examCardVoList = examService.getExamCardList(type,ifAi);
            resultVO = new ResultVO<>(0, "获取考试列表卡片成功", examCardVoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultVO = new ResultVO<>(-1, "获取考试列表卡片失败", null);
        }
        return resultVO;
    }

    @GetMapping("/detail/{id}")
    @Operation(summary ="根据考试的id，获取考试详情")
    ResultVO<ExamDetailVo> getExamDetail(@PathVariable String id) {
        // 根据id获取考试详情
        ResultVO<ExamDetailVo> resultVO;
        try {
            ExamDetailVo examDetail = examService.getExamDetail(id);
            resultVO = new ResultVO<>(0, "获取考试详情成功", examDetail);
        } catch (Exception e) {
            resultVO = new ResultVO<>(-1, "获取考试详情失败", null);
        }
        return resultVO;
    }
    //todo:这里可增加逻辑，使得对考试某次结果的分析进行一个可视乎并返回
    @PostMapping("/finish/{examId}")
    @Operation(summary ="根据用户提交的答案对指定id的考试判分")
    ResultVO<ExamRecordAnalysisVo> finishExam(@PathVariable String examId, @RequestBody HashMap<String, List<String>> answersMap, HttpServletRequest request) {
        ResultVO<ExamRecordAnalysisVo> resultVO;
        try {
            // todo:拦截器里设置上的用户id
            String userId = (String) request.getAttribute("user_id");
            // 下面根据用户提交的信息进行判分,返回用户的得分情况
            ExamRecordAnalysisVo examRecordvo = examService.judge(userId, examId, answersMap);
            resultVO = new ResultVO<>(0, "考卷提交成功", examRecordvo);
        } catch (Exception e) {
            e.printStackTrace();
            resultVO = new ResultVO<>(-1, "考卷提交失败", null);
        }
        return resultVO;
    }
    @GetMapping("/record/list")
    @Operation(summary ="获取当前用户的考试记录")
    ResultVO<List<ExamRecordVo>> getExamRecordList(@RequestParam(required = false,defaultValue = "null")String type,HttpServletRequest request) {
        //todo:获得某个用户的考试记录表，然后整理可以以同样的方式进行一个学生信息的可视化分析
        ResultVO<List<ExamRecordVo>> resultVO;
        try {
            // 拦截器里设置上的用户id
            String userId = (String) request.getAttribute("user_id");
            // 下面根据用户账号拿到他(她所有的考试信息)，注意要用VO封装下
            List<ExamRecordVo> examRecordVoList = examService.getExamRecordList(userId,type);
            resultVO = new ResultVO<>(0, "获取考试记录成功", examRecordVoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultVO = new ResultVO<>(-1, "获取考试记录失败", null);
        }
        return resultVO;
    }

    @GetMapping("/record/detail/{recordId}")
    @Operation(summary ="根据考试记录id获取考试记录详情")
    ResultVO<RecordDetailVo> getExamRecordDetail(@PathVariable String recordId) {
        //todo:这里其实是去点击过去的某一场考试的，然后就可以看到记录了
        ResultVO<RecordDetailVo> resultVO;
        try {
            RecordDetailVo recordDetailVo = examService.getRecordDetail(recordId);
            resultVO = new ResultVO<>(0, "获取考试记录详情成功", recordDetailVo);
        } catch (Exception e) {
            e.printStackTrace();
            resultVO = new ResultVO<>(-1, "获取考试记录详情失败", null);
        }
        return resultVO;
    }

    @GetMapping("/types")
    @Operation(summary ="获取试卷的所有分类")
    ResultVO<List> getExamTypes() {
        ResultVO<List> resultVO;
        try {
            List<String> list = examService.getExamTypes();
            resultVO = new ResultVO<>(0, "获取试卷分类成功", list);
        } catch (Exception e) {
            resultVO = new ResultVO<>(-1, "获取试卷分类失败", null);
        }
        return resultVO;
    }

    @GetMapping("/TypeToExam")
    @Operation(summary ="获取对应分类的试卷")
    ResultVO<List<ExamVo>> getExamType(@RequestParam String type,@RequestParam(required = false,defaultValue = "false") Boolean ifAi) {
        ResultVO<List<ExamVo>> resultVO;
        try {
            List<ExamVo> list = examService.getTypeToExam(type,ifAi);
            resultVO = new ResultVO<>(0, "获取试卷分类成功", list);
        } catch (Exception e) {
            resultVO = new ResultVO<>(-1, "获取试卷分类失败", null);
        }
        return resultVO;
    }

    //根据用户评价生成相对应的题库
    //这里生成后不用返回什么，前端去刷新一下我的题库即可。
    //1.这里的话要不然改一下逻辑，当交完卷之后，在"我的题库"那里有一个按钮，可以点击生成题库
    //2.点击后，会展示用户的考试记录[来个弹窗页面，然后上面展示考试记录卡片]，然后用户可以选择几套试卷，传过来list集合，然后里面是examId【会根据传来的试卷去生成
    //3.然后这里的话生成题库，然后上面有个导航栏：我的题库：这里其实就是展示生成的个人题库的考试卡片【嗯嗯就是这样】
    // 4.然后点击即可做题

    // 这里去生成examId的数组
    //前端：
    /*var examIdsList = [1,2,3];
        $.get({
            url: "/test.htm",
             data: {examIdsList: examIdsList}, 这里组合以要用同名
             success: function (data) {
                     //do something
            }
         });
*/
    //也就是说这里传来的只能说同一种type类型的试卷
    //这里的话就成功了
    @GetMapping("/generateExam")
    @Operation(summary ="根据用户的答题历史生成对应的题库")
    ResultVO<Exam> generateExam(@RequestParam("examIdsList") List<String> examIdsList) throws JsonProcessingException {
        Exam exam = examService.generateExam(examIdsList);
        ResultVO<Exam> resultVO;
        if(exam==null){
            resultVO = new ResultVO<>(-1, "生成试卷失败", null);

        }
        resultVO = new ResultVO<>(0, "生成试卷成功", exam);
        return resultVO;
    }


}
